<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
      class="form-search"
    >
      <el-form-item label="库位编号" prop="binCode">
        <el-input
          v-model="queryParams.binCode"
          placeholder="请输入编号"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="matTitle + '编号'" prop="matCode">
        <el-input
          v-model="queryParams.matCode"
          :placeholder="`请输入${matTitle}编号`"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="matTitle + '名称'" prop="matName">
        <el-input
          v-model="queryParams.matName"
          :placeholder="`请输入${matTitle}名称`"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockList" border>
      <el-table-column
        v-for="(item, index) in columns"
        :key="index"
        :label="item.label"
        align="center"
        :prop="item.prop"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listStock } from "@/api/warehouse/stock";

export default {
  name: "StockTable",
  props: {
    columns: {
      type: Array,
      default: () => [],
    },
    code: {
      type: String,
      default: "",
    },
    type: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实时库存表格数据
      stockList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        factory: undefined,
        workshopCode: undefined,
        lineCode: undefined,
        sectionCode: undefined,
        pageNum: 1,
        pageSize: 10,
        matCode: null,
        matName: null,
        batchNo: null,
        storehouseCode: null,
        binCode: null,
        packing: null,
        packageUnit: null,
      },
    };
  },
  computed: {
    matTitle() {
      return this.type == "mat" ? "物料" : "产品";
    },
  },
  watch: {
    code(newValue) {
      if (newValue) {
        this.getList();
      }
    },
  },
  methods: {
    /** 查询实时库存列表 */
    getList() {
      this.loading = true;
      const params = {
        ...this.queryParams,
        storehouseCode: this.code,
        stockType: this.type,
      };
      listStock(params).then((response) => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
